//
//  AppDelegate.swift
//  DragonWallet
//
//  Created by volantPig on 2023/12/26.
//

import UIKit
import IQKeyboardManagerSwift
import SwiftyJSON

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        if let loginInfoStr = UserDefaults.standard.string(forKey: "loginInfo") {
            if let loginModel = JDLoginModel.deserialize(from: loginInfoStr, designatedPath: "data") {
                JDLoginModel.default.refreshData(loginModel)
            }
        }
        
        if #available(iOS 15.0, *) {
            UITableView.appearance().sectionHeaderTopPadding = CGFLOAT_MIN
        } else {
            // Fallback on earlier versions
        }
        JDWechatManager.default.initSDK()
        
        OpenInstallSDK.initWith(self)
        
        IQKeyboardManager.shared.isEnabled = true
        
        IQKeyboardManager.shared.resignOnTouchOutside = true//shouldResignOnTouchOutside = true // 当点击键盘外部时，键盘是否应该关闭

        IQKeyboardManager.shared.keyboardDistance = 10//keyboardDistanceFromTextField = 10 // 输入框距离键盘的距离
        
//        self.window = UIWindow.init(frame: UIScreen.main.bounds)
//
//        // 创建第一个视图控制器
//        let advertViewController = JDAdvertLaunchViewController()
//        advertViewController.view.backgroundColor = UIColor.commonColor()
//        self.window?.rootViewController = advertViewController
//
//        if #available(iOS 13.0, *) {
//            self.window?.overrideUserInterfaceStyle = .light
//        } else {
//            // Fallback on earlier versions
//        }
//
//        // 显示window
//        self.window?.makeKeyAndVisible()
                
        // Override point for customization after application launch.
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
    
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }
    
    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([any UIUserActivityRestoring]?) -> Void) -> Bool {
        OpenInstallSDK.continue(userActivity)
//        return WXApi.handleOpenUniversalLink(userActivity, delegate: self)
        return true
    }
    

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        
        print("这里触发了没有啊")
        
        if url.host == "safepay" {
            printLog("这里触发了")
//            AlipaySDK.defaultService().processOrder(withPaymentResult: url) { result in
//                print("支付宝回调信息:\(result)")
//                
//            }
            
            AlipaySDK.defaultService()?.processOrder(withPaymentResult: url, standbyCallback: { (resultDic) in
//                [AnyHashable : Any]
//                let orderState = JSON(resultDic)["resultStatus"].intValue
                let orderState = JSON(resultDic?["resultStatus"] as Any).intValue
                switch orderState {
                case 8000:
                    print("支付正在处理中")
                case 4000:
                    print("支付失败")
                case 6001:
                    print("支付取消")
                case 6002:
                    print("网络错误")
                case 9000:
                    print("支付成功")
                default:
                    break
                }
            
                print("支付宝支付回调")
            })
            
            
        }
        
        
        
        
        return true
    }
}


extension AppDelegate: OpenInstallDelegate {
//    func getWakeUpParams(_ appData: OpeninstallData?) {
//        print("他来了，他来了")
//        guard let openinstallInfo = appData else { return}
//        print("我要看的数据:\(openinstallInfo.data)和:\(openinstallInfo.channelCode)")
//    }
    
    
    
}


/**
 *  快登授权2.0
 *
 *  @param infoStr          授权请求信息字串
 *  @param schemeStr        调用授权的app注册在info.plist中的scheme
 *  @param completionBlock  授权结果回调，需要调用方在appDelegate中调用processAuth_V2Result:standbyCallback:方法获取授权结果
 *                          若在授权过程中,调用方应用被系统终止则此block无效(此时会调用'processAuth_V2Result:standbyCallback:'传入的standbyCallback)
 */
